package constellation.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.myapplication.R;
import com.google.gson.Gson;

import java.util.List;
import java.util.Map;

import constellation.Activity.meAboutActivity;
import constellation.Activity.meFeedbackActivity;
import constellation.Activity.meFunctioActivity;
import constellation.Activity.meUpdateLogActivity;
import constellation.adapter.ItemDialogGridviewAdapter;
import constellation.adapter.LuckBaseAdapter;
import constellation.custom.DLRoundImageView;
import constellation.util.AssetsUtil;
import constellation.util.StartInfo;

/**
 * A simple {@link Fragment} subclass.
 */
public class meFragment extends Fragment implements View.OnClickListener {
    Context context;
    View view;
    private DLRoundImageView meIv;
    private TextView meTv;
    private LinearLayout layoyt;
    private TextView meSoftTv;
    private TextView meFunctionTv;
    private TextView meUpdateTv;
    private TextView meFeedbackTv;
    List<StartInfo.StarinfoBean> list;
    Map<String, Bitmap> contentlogoImgMap;
    SharedPreferences preferences;
    SharedPreferences.Editor editor;

    public meFragment() {
        // Required empty public constructor
    }

    int index;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        context = getContext();
        view = inflater.inflate(R.layout.fragment_me, container, false);
        preferences=context.getSharedPreferences("data",Context.MODE_PRIVATE);
        editor=preferences.edit();
        index=preferences.getInt("index",0);
        initView();
        initData();
        return view;
    }

    public void initData() {
        //将json字符串转换成创建类的对象
        Bundle bundle = getArguments();
        StartInfo starInfo = (StartInfo) bundle.getSerializable("info");
        list = starInfo.getStarinfo();  //数据源
        String logoname = list.get(index).getLogoname();
        contentlogoImgMap = AssetsUtil.getContentlogoImgMap();
        Bitmap bm = contentlogoImgMap.get(logoname);
        meIv.setImageBitmap(bm);
        meTv.setText(list.get(index).getName());
//        setImageListener();

    }

    public void setImageListener() {

                GridView meDialogGridview;
                ItemDialogGridviewAdapter adapter;
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            View view1 = View.inflate(context, R.layout.me_dialog, null);
            builder.setView(view1);
            meDialogGridview = (GridView)view1.findViewById(R.id.me_dialog_gridview);
            adapter=new ItemDialogGridviewAdapter(context,list);
            meDialogGridview.setAdapter(adapter);
            final AlertDialog show= builder.show();
            meDialogGridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String logoname = list.get(position).getLogoname();
                    contentlogoImgMap = AssetsUtil.getContentlogoImgMap();
                    Bitmap bm2 = contentlogoImgMap.get(logoname);
                    meIv.setImageBitmap(bm2);
                    meTv.setText(list.get(position).getName());
                    editor.putInt("index", position);
                    editor.apply();
                    show.dismiss();
                }
    });
}

    private void initView() {

        meIv = (DLRoundImageView) view.findViewById(R.id.me_iv);

        meTv = (TextView) view.findViewById(R.id.me_tv);
        layoyt = (LinearLayout) view.findViewById(R.id.layoyt);
        meSoftTv = (TextView) view.findViewById(R.id.me_soft_tv);
        meFunctionTv = (TextView) view.findViewById(R.id.me_function_tv);
        meUpdateTv = (TextView) view.findViewById(R.id.me_update_tv);
        meFeedbackTv = (TextView) view.findViewById(R.id.me_feedback_tv);

        meSoftTv.setOnClickListener(this);
        meUpdateTv.setOnClickListener(this);
        meFunctionTv.setOnClickListener(this);
        meFeedbackTv.setOnClickListener(this);
        meIv.setOnClickListener(this);
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.me_iv:
                setImageListener();
                break;
            case R.id.me_soft_tv:
                startActivity(new Intent(context, meAboutActivity.class));
                break;
            case R.id.me_function_tv:
                startActivity(new Intent(context, meFunctioActivity.class));
                break;
            case R.id.me_update_tv:
                startActivity(new Intent(context, meUpdateLogActivity.class));
                break;
            case R.id.me_feedback_tv:
                startActivity(new Intent(context, meFeedbackActivity.class));
                break;
        }
    }
}
